package org.jeecg.modules.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.entity.FavApply;
import org.jeecg.modules.entity.FavCategory;
import org.jeecg.modules.entity.FavVersion;
import org.jeecg.modules.service.IFavApplyService;
import org.jeecg.modules.service.IFavCategoryService;
import org.jeecg.modules.service.IFavVersionService;
import org.jeecg.modules.utils.FavCache;
import org.jeecg.modules.utils.NetworkRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @program: jeecg-boot
 * @description: 开放接口
 * @author: wei
 * @create: 2022-11-18 20:48
 **/
@Api(tags = "开放接口")
@RestController
@RequestMapping("/open")
@Slf4j
public class openController {

    @Autowired
    private IFavCategoryService favCategoryService;

    @Autowired
    private IFavApplyService favApplyService;

    @Autowired
    private IFavVersionService favVersionService;

    /**
     * @return org.jeecg.common.api.vo.Result<java.lang.Object>
     * @author wei
     * @description 获取分类列表缓存
     * @date 2022/11/18 22:20
     */
    @ApiOperation(value = "获取分类列表缓存", notes = "获取分类列表缓存")
    @GetMapping(value = "/getCategoryList")
    @Cacheable(cacheNames = FavCache.CATEGORY_LIST)
    public Result<Object> getCategoryList() {
        QueryWrapper<FavCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        List<FavCategory> list = favCategoryService.list(queryWrapper);
        return Result.OK(list);
    }

    /**
     * @return org.jeecg.common.api.vo.Result<java.lang.Object>
     * @author wei
     * @description 获取应用列表
     * @date 2022/11/18 22:20
     */
    @ApiOperation(value = "获取应用列表缓存", notes = "获取应用列表缓存")
    @GetMapping(value = "/getApplyList")
    @Cacheable(cacheNames = FavCache.APPLY_LIST, key = "#type")
    public Result<Object> getApplyList(@RequestParam(name = "type", defaultValue = "") String type) {

        QueryWrapper<FavApply> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(type) && !type.equals("-1")) {
            queryWrapper.eq("type", type);
        }
        if (type.equals("-1")) {
            queryWrapper.eq("is_recommend", "Y");
        }
        List<FavApply> list = favApplyService.list(queryWrapper);

        return Result.OK(list);
    }

    /**
     * @return org.jeecg.common.api.vo.Result<java.lang.Object>
     * @author wei
     * @description 请求URL信息
     * @date 2022/11/21 17:06
     */
    @ApiOperation(value = "请求URL信息", notes = "请求URL信息")
    @PostMapping(value = "/submitURL")
    public Result<Object> submitURL(@RequestBody Map<String, String> map) {
        String url = map.getOrDefault("url", null);
        if (StringUtils.isEmpty(url)) {
            return Result.error("网址不能为空");
        }

        if (!NetworkRequest.isHttpOrHttps(url)) {
            return Result.error("错误网址");
        }

        Map<String, String> mapRes = NetworkRequest.getContentUrl(url);
        if (ObjectUtil.isEmpty(mapRes)) {
            return Result.error("链接无法识别");
        }
        mapRes.put("url", url);

        return Result.ok(mapRes);
    }

    @ApiOperation(value = "获取应用列表缓存", notes = "获取应用列表缓存")
    @GetMapping(value = "/getVersionQuery")
    @Cacheable(cacheNames = FavCache.VERSION_CONTROL, key = "#type")
    public Result<Object> getVersionQuery(@RequestParam(value = "type", defaultValue = "") String type) {

        FavVersion version = new FavVersion();
        version.setType(type);

        QueryWrapper<FavVersion> queryWrapper = new QueryWrapper<>(version);
        version = favVersionService.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(version)) {
            return Result.ok();
        }
        return Result.ok(version.getCode());
    }
}
